<template>
  <el-dialog
    :title="titleShow" :close-on-click-modal="false" :visible.sync="visible"
    @close="clearDataList()" width="900px" top="8vh">
    <basic-container>
      <div style="height: 30vh; overflow-y: auto;">
        <el-row>
          <el-col :span="24">
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="140px">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="个人考核分级" prop="config03">
                    <el-select :clearable="true" v-model="dataForm.config03" placeholder="请选择" class="xiaLa" style="width:200px">
                      <el-option v-for="(item,index) in lxOptions" :key="index" :label="item.label" :value="item.label">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="工时(工作量)奖励" prop="config01">
                    <el-input-number v-model="dataForm.config01" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="员工考核基础分" >
                    <el-input-number v-model="dataForm.config02" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="员工月绩效考核分" >
                    <el-input-number v-model="dataForm.config04" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效年薪基数" >
                    <el-input-number v-model="dataForm.config05" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="综合业绩分值" >
                    <el-input-number v-model="dataForm.config06" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效奖金基数" >
                    <el-input-number v-model="dataForm.config07" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="个人绩效分数" >
                    <el-input-number v-model="dataForm.config08" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效分值指数" >
                    <el-input-number v-model="dataForm.config09" :precision="2" :step="1"  :min="0" style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-col>
        </el-row>
        <el-row v-show="false">
          <el-col :span="24">
            <el-table  :data="jjxData" stripe border width="99%">
              <el-table-column prop="" label="序号" header-align="center" align="center" :formatter="nShowIndex" width="70px"></el-table-column>
              <el-table-column prop="jjxName" header-align="center" align="center" label="奖金项名称"></el-table-column>
              <el-table-column prop="value" header-align="center" align="center" label="员工岗位系数">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p11" :precision="2" :step="1"  :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column  header-align="center" align="center" label="每月员工奖金">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p12" :precision="2" :step="1"  :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column  header-align="center" align="center" label="奖励标准奖励">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p13" :precision="2" :step="1"  :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </div>
    </basic-container>
    <span slot="footer" class="dialog-footer">
      <el-button @click="btnDefault" type="primary" plain>恢复默认</el-button>
      <el-button @click="btnZero" type="danger" plain>清零</el-button>
      <el-button type="primary" @click="btnConfirm()" v-if="canSubmit">保存</el-button>
      <el-button @click="clearDataList" type="primary" plain>关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
/*!
 * 系数配置_分组_全部
 *
 * @author lxl
 * @date 2021-9-6 09:02:21
 */
import {allocationRyxxbFz,allocationRyxxbQb,getRyxxbByCode} from '@/api/jiangjin/jiangjinryxxb'
import {getJjDeptJjx} from '@/api/jiangjin/jiangjinjjxxxb'

export default {
  data(){
    return{
      visible:false,
      canSubmit: false,
      fzCode:"",
      deptId:'',
      jjxData:[],
      titleShow:'系数设置',

      dataForm: {
        config01:'',
        config02:'',
        config03:'',
        config04:'',
        config05:'',
        config06:'',
        config07:'',
        config08:'',
        config09:'',

        config11:'',
        config12:'',
        config13:'',
      },
      dataRule: {
        config03: [
          { required: true, message: '请选择个人考核分级', trigger: 'blur' }
        ],
      },
      lxOptions: [{value: 'A',label: 'A' }, { value: 'B',label: 'B' },
        {value: 'C',label: 'C' }, { value: 'D',label: 'D' }],
    }
  },
  methods:{
    /* 1、配置中默认系数、指数为1
       2、配置中默认分值为100
       3、配置中默认基数为1000
       4、其他数设置1000
       */
    btnDefault(){
      this.dataForm.config01 = 1000;
      this.dataForm.config02 = 100;
      this.dataForm.config04 = 100;
      this.dataForm.config05 = 1000;

      this.dataForm.config06 = 100;
      this.dataForm.config07 = 1000;
      this.dataForm.config08 = 100;
      this.dataForm.config09 = 1;

      for(let item of this.jjxData){
        item.p11 = 1;
        item.p12 = 1000;
        item.p13 = 1000;
      }
    },
    //清零
    btnZero(){
      this.dataForm.config01 = '';
      this.dataForm.config02 = '';
      this.dataForm.config04 = '';
      this.dataForm.config05 = '';

      this.dataForm.config06 = '';
      this.dataForm.config07 = '';
      this.dataForm.config08 = '';
      this.dataForm.config09 = '';


      for(let item of this.jjxData){
        item.p11 = '';
        item.p12 = '';
        item.p13 = '';
      }
    },
    init (code,deptId,info) {
      //分组配置系数时fzCode为分组的id，全部配置时fzCode值为'all'
      this.fzCode = code;
      this.deptId = deptId;

      this.visible = true;
      this.canSubmit = true;

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
          getJjDeptJjx(deptId).then(res =>{
            const xData = res.data.data;
            this.jjxData = [];
            xData.map(item =>{
              this.jjxData.push({
                jjxId: item.id,
                jjxName: item.jjxMc,
                p11:this.setConfig(item.id,11),//'',
                p12:this.setConfig(item.id,12),//'',
                p13:this.setConfig(item.id,13),//'',
                value:''})
            })
          })
        if(info){
          getRyxxbByCode(info).then(response => {
            if(response.data.data){
              this.dataForm.config01 = response.data.data.config01;
              this.dataForm.config02 = response.data.data.config02;
              this.dataForm.config03 = response.data.data.config03;
              this.dataForm.config04 = response.data.data.config04;
              this.dataForm.config05 = response.data.data.config05;
              this.dataForm.config06 = response.data.data.config06;
              this.dataForm.config07 = response.data.data.config07;
              this.dataForm.config08 = response.data.data.config08;
              this.dataForm.config09 = response.data.data.config09;
            }
          })
        }
      })
    },
    setConfig(id,vm){
      let lTemp = [];
      let lResult = "";

      if((typeof this.fzCode)=='string'&&this.fzCode!='all'){
        if(vm === 11 && this.dataForm.config11 != null) { //员工岗位系数
          lTemp =  JSON.parse(this.dataForm.config11);
        }else if(vm === 12 && this.dataForm.config12 != null){//每月员工奖金
          lTemp =  JSON.parse(this.dataForm.config12);
        }else if(vm === 13 && this.dataForm.config13 != null) {//奖励标准奖励
          lTemp =  JSON.parse(this.dataForm.config13);
        }
      }

      for(let i=0;i<lTemp.length;i++){
        if(lTemp[i].jjxId === id){
          lResult = lTemp[i].value;
          break;
        }
      }

      return lResult;
    },
    nShowIndex(row, column, cellValue, index){
      return index + 1;
    },

    getConfig(vm){
      let lResult = [];
      if(vm === 11){ //员工岗位系数
        this.jjxData.map(item =>{
          lResult.push({
            jjxId:item.jjxId,
            jjxName:item.jjxName,
            value:item.p11.toFixed(2)
          })
        })
      }else if(vm === 12){//每月员工奖金
        this.jjxData.map(item =>{
          lResult.push({
            jjxId:item.jjxId,
            jjxName:item.jjxName,
            value:item.p12.toFixed(2)
          })
        })
      }else if(vm === 13){//奖励标准奖励
        this.jjxData.map(item =>{
          lResult.push({
            jjxId:item.jjxId,
            jjxName:item.jjxName,
            value:item.p13.toFixed(2)
          })
        })
      }
      return lResult;
    },
    btnConfirm(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$set(this.dataForm,"config11",JSON.stringify(this.getConfig(11)));
          this.$set(this.dataForm,"config12",JSON.stringify(this.getConfig(12)));
          this.$set(this.dataForm,"config13",JSON.stringify(this.getConfig(13)));
          this.$set(this.dataForm,"deptId",this.deptId);



          this.canSubmit = false;

          if((typeof this.fzCode)=="number"){
            //分组
            allocationRyxxbFz(this.dataForm,this.fzCode).then(res =>{
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.canSubmit = true;
              this.$emit("refresh");
            }).catch(() => {
              this.canSubmit = true;
            });
          }else if(this.fzCode=='all'){
            //全部
            allocationRyxxbQb(this.dataForm).then(res =>{
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.canSubmit = true;
              this.$emit("refresh");
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    clearDataList(){
      this.initNone();
      this.visible = false;
    },
    //初始化
    initNone(){
      this.fzCode = '';
      this.dataForm ={};
      this.dataForm = {
        config01:'',
        config02:'',
        config03:'',
        config04:'',
        config05:'',
        config06:'',
        config07:'',
        config08:'',
        config09:'',

        config11:'',
        config12:'',
        config13:'',
      };
      this.jjxData =[]
    }

  }
}
</script>

<style scoped>

</style>
